<?php
// +-------------------------------------------------------
// | 多开限制后置中间件
// +-------------------------------------------------------

declare(strict_types=1);

namespace app\common\middleware;

use liang\MicroEngine;
use app\common\logic\Platform;

class Batch
{
    /**
     * 处理请求
     *
     * @param \think\Request $request
     * @param \Closure       $next
     * @return Response
     */
    public function handle($request, \Closure $next)
    {
        // 后置中间件
        $response = $next($request);
        if (MicroEngine::isMicroEngine() && app('http')->getName() == 'admin') {
            if (parse_name($request->controller()) !== 'platform') {
                if (!Platform::initData()) {
                    // 触发多开限制
                    return redirect(u('platform/index'));
                }
            } else {
                if (Platform::initData()) {
                    // 没有多开却访问触发多开限制提示页面重定向到后台主页
                    return redirect(u('index/index'));
                }
            }
        }
        return $response;
    }
}
